package com.dz.scrm.mvp.ui;

import android.content.Intent;
import android.os.Bundle;

import com.dz.scrm.push.JPushHelper;
import com.dz.scrm.R;
import com.dz.scrm.app.adpter.companyAdpter;
import com.dz.scrm.mvp.model.entity.CompanyInfoBean;
import com.dz.scrm.mvp.model.entity.LoginBean;
import com.dz.scrm.mvp.model.entity.LoginCompanyBean;
import com.dz.scrm.mvp.presenter.UserPresenter;
import com.dz.scrm.utils.KingSoftParasJson;
import com.dz.scrm.utils.LoggerUtils;
import com.dz.scrm.utils.SharedPreferencesUtil;
import com.dz.scrm.utils.StringUtils;
import com.google.gson.Gson;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import me.jessyan.art.integration.AppManager;
import me.jessyan.art.mvp.IView;
import me.jessyan.art.mvp.Message;
import me.jessyan.art.utils.ArtUtils;

import static com.dz.scrm.app.config.MessageWhatTagsKt.WHAT_CHECK_COMPANY_SUCCESS;
import static com.dz.scrm.utils.SharedPreferencesUtil.RegistrationID;

/**
 * @Description: 类作用描述
 * @Author: 黄海
 * @CreateDate: 2021-4-22 17:15
 * @UpdateUser: 黄海
 * @UpdateDate: 2021-4-22 17:15
 * @UpdateRemark: 更新说明
 */
public class ChooseCompany extends DZActivity<UserPresenter> implements IView , companyAdpter.itemOnClickListener {
    private List<LoginCompanyBean> loginCompanyBeans;
    private companyAdpter adpter;
    private String phoneNum,verify;
    private LoginBean loginBean;
    @BindView(R.id.company_recyclerview)
    RecyclerView company_recyclerview;

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_choose_company;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        String info = SharedPreferencesUtil.getInfo(SharedPreferencesUtil.LOGIN_INFO);
        if (!StringUtils.isEmpty(info)){
            loginBean = (LoginBean) KingSoftParasJson.getObjectByJson(info,LoginBean.class);
            if (loginBean != null){
                phoneNum = loginBean.getPhoneNum();
                verify = loginBean.getVerify();
                loginCompanyBeans = loginBean.getList();
                if (loginCompanyBeans != null){
//                    Log.e("HH","LOG => "+new Gson().toJson(loginCompanyBeans));
                    adpter = new companyAdpter(this,loginCompanyBeans,this);
                    company_recyclerview.setLayoutManager(new GridLayoutManager(this, 1));
                    company_recyclerview.setAdapter(adpter);
                }
            }
        }
    }

    @Nullable
    @Override
    public UserPresenter obtainPresenter() {
        return new UserPresenter(ArtUtils.obtainAppComponentFromContext(this),new RxPermissions(this));
    }

    @Override
    public void showMessage(@NonNull String message) {
        ArtUtils.snackbarText(message);
    }

    @Override
    public void handleMessage(@NonNull Message message) {
        switch (message.what) {
            case WHAT_CHECK_COMPANY_SUCCESS:
                //企业验证成功
                CompanyInfoBean companyInfoBean = (CompanyInfoBean) message.obj;
                if (message != null && message.obj != null) {
                    //保存企业信息
                    if (companyInfoBean != null) {
                        isHidenPhoneNum(companyInfoBean.is_hidden());
                        SharedPreferencesUtil.saveInfo(SharedPreferencesUtil.COMPANY_INFO,new Gson().toJson(companyInfoBean));
                        SharedPreferencesUtil.saveInfo(SharedPreferencesUtil.TOKEN,companyInfoBean.getAccess_token());
                        Intent intent = new Intent();
                        intent.setClass(this,HomeActivity.class);
                        startActivity(intent);
                        AppManager.getAppManager().killAll();
                    }
                }
        }
    }

    @Override
    public void onClick(int position) {
        if (loginCompanyBeans != null){
            if (position < loginCompanyBeans.size()){
                Message msg =  Message.obtain(this);
                LoginCompanyBean bean = loginCompanyBeans.get(position);
                if (bean != null){
                    LoggerUtils.d("checkCompany：" + SharedPreferencesUtil.getInfo(RegistrationID));
                    mPresenter.checkCompany(msg,phoneNum,bean.getTenantId(),verify, JPushHelper.INSTANCE.getRegistrationID(this));
                }
            }
        }
    }
}
